﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

using ALE_Server;
using ALE_Server.Plugin;

namespace ALE_Server.Plugin.Notifiers
{
    public class TCPNotifier : Notifier
    {
        private string m_Host;
        private int m_Port;

        public TCPNotifier(string uri)
            : base(uri)
        {
        }

        public override void Init(string uri)
        {
            m_Uri = new Uri(uri);
            m_Host = m_Uri.Host;
            m_Port = m_Uri.Port;
        }

        public override void NotifySubscriber(ALE_Server.EC.ECReports reports)
        {
            try
            {
                TcpClient Client = new TcpClient(m_Host,m_Port);

                NetworkStream Stream = Client.GetStream();
                WriteToOutputStream(reports, Stream);
                Stream.Close();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        public override string GetURIFromFields()
        {
            StringBuilder Result = new StringBuilder();
            Result.Append("tcp://");
            Result.Append(m_Host);
            Result.Append(":");
            Result.Append(m_Port);

            return Result.ToString();
        }
    }
}
